//
//  NotesAllCommentsTableViewCell.m
//  TravelFirst
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "NotesAllCommentsTableViewCell.h"

@implementation NotesAllCommentsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        
        self.avatorButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.avatorButton.frame = CGRectMake(10, 10, 40, 40);
        self.avatorButton.backgroundColor = [UIColor blueColor];
        self.avatorButton.layer.masksToBounds = YES;
        self.avatorButton.layer.cornerRadius = self.avatorButton.width / 2;
        [self.contentView addSubview:self.avatorButton];
        
        self.contentLabel = [[UILabel alloc] init];
        //self.contentLabel.backgroundColor = [UIColor redColor];
        self.contentLabel.numberOfLines = 0;
        [self.contentView addSubview:self.contentLabel];
        
        self.pictureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.pictureButton.frame = CGRectMake(kWidth - 40 - 10, 10, 40, 40);
        self.pictureButton.layer.masksToBounds = YES;
        self.pictureButton.layer.cornerRadius = 2;
        //self.pictureButton.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.pictureButton];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = kColor;
        lineView.tag = 100;
        [self.contentView addSubview:lineView];
    }
    return self;
}

- (void)setValueWithModel:(NotesModel *)model
{
    [self.avatorButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.avatar] forState:UIControlStateNormal placeholderImage:nil];
    [self.pictureButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.picture] forState:UIControlStateNormal placeholderImage:nil];
    [self setStringAttributeWithModel:model];
    
}

- (void)setTarget:(id)target avatorButtonAction:(SEL)avatorButtonAction pictureButtonAction:(SEL)pictureButtonAction tag:(NSInteger)tag
{
    self.avatorButton.tag = 100 + tag;
    [self.avatorButton addTarget:target action:avatorButtonAction forControlEvents:UIControlEventTouchUpInside];
    self.pictureButton.tag = 1000 + tag;
    [self.pictureButton addTarget:target action:pictureButtonAction forControlEvents:UIControlEventTouchUpInside];
}

- (void)setStringAttributeWithModel:(NotesModel *)model
{
    NSMutableAttributedString *nameAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@: ", model.nickname]];
    [nameAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, model.nickname.length + 1)];
    
    NSString *createdDate = [NotesAllCommentsTableViewCell transformDateWithString:model.created_date];
    NSMutableAttributedString *contentAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@", model.content, createdDate]];
    [contentAttStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(model.content.length + 1, createdDate.length)];
    
    [nameAttStr appendAttributedString:contentAttStr];
    self.contentLabel.attributedText = nameAttStr;
    
    CGFloat height = [NotesAllCommentsTableViewCell getLabelHeightWithString:self.contentLabel.text];
    
    self.contentLabel.frame = CGRectMake(self.avatorButton.right + 10, self.avatorButton.top + 3, kWidth - self.avatorButton.right - 10 - 40 - 10 - 10, height);
    
    
    UIView *lineView = [self.contentView viewWithTag:100];
    lineView.frame = CGRectMake(self.avatorButton.left, self.contentLabel.bottom + 10, kWidth - 10, 1);
}

+ (CGFloat)getLabelHeightWithString:(NSString *)string
{
    return [string boundingRectWithSize:CGSizeMake(225, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName] context:nil].size.height;
}

+ (NSString *)transformDateWithString:(NSString *)string
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[string floatValue]];
    NSString *dateStr = [formatter stringFromDate:date];
    
    return [dateStr substringToIndex:16];
}

// cell高度
+ (CGFloat)appendStringWithModel:(NotesModel *)model
{
    NSString *string = [model.nickname stringByAppendingFormat:@": %@\n%@", model.content, [NotesAllCommentsTableViewCell transformDateWithString:model.created_date]];
    return [NotesAllCommentsTableViewCell getLabelHeightWithString:string];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
